package main.pmc;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import utils.StringUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 葛成瑞
 * @Date: 2025/3/17 16:25
 */
public class CheckHtml {

    @SneakyThrows
    public static void main(String[] args) throws SQLException {

        Integer pmidF = 3738991;
        Db useTaiyi = Db.use(DSFactory.get("local_articleadd"));
        String saveTableFrom = "pmcdownziphtml";
        String paramSql = "select *  from " + saveTableFrom + "  ";
        List<Entity> query = useTaiyi.query(paramSql);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                20,
                50,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>());
        for (Entity entity : query) {
            executor.submit(() -> {
                int pmid = entity.getInt("pmid");
                System.out.println("查询的pmid" + pmid);
                byte[] htmls = entity.getBytes("html");
                if (StringUtil.isNotNull(htmls)) {
                    String html = ZipUtil.unGzip(htmls, "UTF-8");
                    Document parse = Jsoup.parse(html);
                    Element element = parse.selectFirst("div:containsOwn(PMCID:)");
                    if (element != null) {
                        String text = element.text();
                        int pmidRealPmid = 0;
                        try {
                            pmidRealPmid = Integer.parseInt(text.split("PMID:")[1].trim());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (pmidRealPmid != 0 && pmidRealPmid != pmid) {
                            System.out.println("pmid 不一致" + pmid + "---" + pmidRealPmid);
                            entity.set("real_pmid", pmidRealPmid);
//                            try {
//                                useTaiyi.insertOrUpdate(entity, "pmid");
//                            }catch (Exception e){
//                                e.printStackTrace();
//                            }
                        }
                    }
                }
            });
        }
        executor.shutdown();
        while (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
            int activeCount = executor.getActiveCount();
            System.out.println("当前线程数" + activeCount);
            long completedTaskCount = executor.getCompletedTaskCount();
            System.out.println("已完成任务数" + completedTaskCount);
        }

    }
}
